package com.shoppingmall.fragment;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;

import com.example.shoppingmall.R;
import com.shopping.tools.CategoryListTools;
import com.shopping.tools.Constant;
import com.shopping.tools.GetUrlMethod;
import com.shoppingmall.Adapter.HomePromoteActivityAdapter;
import com.shoppingmall.activity.ShopTypeActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.ScrollView;

public class Fragment_Home extends Fragment implements OnCheckedChangeListener {
	private View view;
	private HomePromoteActivityAdapter mHomePromoteActivityAdapter;
	private ListView listview;
	private ScrollView scrollview_home;
	private ActivityThread thread;
	private String req = "";
	private Intent intent;
	private RadioButton rb_repast, rb_entertainment, rb_shopping, rb_lifeserve;
	private String url = "http://malladmin.bl99w.com/index.php?r=activity/listPromote&req=";
	private ArrayList<HashMap<String, String>> activityList = new ArrayList<HashMap<String, String>>();
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_home, container, false);
		init();
		mHomePromoteActivityAdapter = new HomePromoteActivityAdapter(
				getActivity(), activityList);
		listview.setAdapter(mHomePromoteActivityAdapter);
		thread = new ActivityThread();
		thread.start();
		return view;
	}

	private void init() {
		// TODO Auto-generated method stub
		listview = (ListView) view.findViewById(R.id.lv_promoteActivity);
		scrollview_home = (ScrollView) view.findViewById(R.id.scrollview_home);
		scrollview_home.scrollTo(0, 0);
		rb_repast = (RadioButton) view.findViewById(R.id.rb_repast);
		rb_entertainment = (RadioButton) view
				.findViewById(R.id.rb_entertainment);
		rb_shopping = (RadioButton) view.findViewById(R.id.rb_shopping);
		rb_lifeserve = (RadioButton) view.findViewById(R.id.rb_lifeserve);
		rb_repast.setOnCheckedChangeListener(this);
		rb_entertainment.setOnCheckedChangeListener(this);
		rb_shopping.setOnCheckedChangeListener(this);
		rb_lifeserve.setOnCheckedChangeListener(this);
	}

	// 线程
	private class ActivityThread extends Thread {
		public void run() {
			// TODO Auto-generated method stub
			super.run();
			req = "{\"mallId\":1,\"count\":10}";// 推荐活动参数
			try {
				String urlpath = GetUrlMethod.getURLString(url
						+ URLEncoder.encode(req, "utf-8"));
				CategoryListTools.json_PromoteActivity(urlpath, activityList);
				// 对数据进行解析
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			Message msg = new Message();
			msg.what = 1;
			myhadler.sendMessage(msg);// 向myhandler发送消息
		}
	}

	private Handler myhadler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch (msg.what) {
			case 1:
				mHomePromoteActivityAdapter.notifyDataSetChanged();
				break;
			}

		}
	};

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
		if (isChecked)
			switch (buttonView.getId()) {
			case R.id.rb_repast:
				Constant.shopTypeId = 1;
				Constant.shopTypeTitle = "餐饮";
				intent();
				break;
			case R.id.rb_entertainment:
				Constant.shopTypeId = 2;
				Constant.shopTypeTitle = "娱乐";
				intent();
				break;
			case R.id.rb_shopping:
				Constant.shopTypeId = 3;
				Constant.shopTypeTitle = "购物";
				intent();
				break;
			case R.id.rb_lifeserve:
				Constant.shopTypeId = 4;
				Constant.shopTypeTitle = "生活服务";
				intent();
				break;
			default:
				break;

			}
	}

	private void intent() {
		intent = new Intent(getActivity(), ShopTypeActivity.class);
		startActivity(intent);
	}

}
